package com.club.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.club.bean.Administrator;
import com.club.service.AdministratorService;
import com.club.service.RedisService;
import com.club.util.DeCodeUtil;
import com.club.util.JWTUtil;
import com.club.util.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("adminstrator")
@CrossOrigin
public class AdministratorController {
    //注入AdministratorService
    @Autowired
    AdministratorService administratorService;
    @Autowired
    RedisService redisService;
    //获取管理员列表
    @GetMapping("list")
    public Result getList(){
        List<Administrator> list=administratorService.list();
        return Result.success(list);
    }
    //分页获取管理员列表
    @GetMapping("page")
    public Result getPage(Page page, @RequestParam(value = "searchText",required = false) String searchText){
        System.out.println(searchText);
        IPage<Administrator> result=null;
        //如果搜索内容为空，则获取全部管理员列表
        if(searchText==null||searchText.trim().isEmpty()) {
            result=administratorService.page(page);
        }else{
            //如果搜索内容不为空，则根据搜索内容获取管理员列表
            QueryWrapper<Administrator> wrapper= new QueryWrapper<>();
            wrapper.like("account", searchText)
                    .or().like("name", searchText)
                    .or().like("department", searchText);
            result=administratorService.page(page,wrapper);
        }
        return Result.success(result);
    }
    //添加管理员
    @PostMapping("add")
    public Result add(@RequestBody Administrator admin){
        //设置默认密码
        admin.setPassword("123456");
        //解密密码
        String pass=admin.getPassword();
        String newpass= DeCodeUtil.deCode(pass);
        admin.setPassword(newpass);
        //保存管理员信息
        administratorService.save(admin);
        //根据账号获取管理员信息
        Administrator admin1=administratorService.getById(admin.getAccount());
        return Result.success("保存成功",admin);
    }
    //删除管理员
    @DeleteMapping("/{account}")
    public Result delete(@PathVariable("account") String account){
        //根据account删除管理员
        boolean b=administratorService.removeById(account);
        //如果删除成功，返回成功信息
        if(b){
            return Result.success("删除成功",null);
        //否则返回错误信息
        }else{
            return Result.error(201,"删除失败");
        }
    }
    @PutMapping("edit")
    //修改管理员信息
    public Result edit(@RequestBody Administrator admin){
        boolean b= administratorService.saveOrUpdate(admin);
        //保存管理员信息
        Administrator admin1=administratorService.getById(admin.getAccount());
        if(b){
            return Result.success("修改成功",admin1);
        }else {
            return Result.error(201,"修改失败");
        }
    }
    @PostMapping("login")
    public Result login(Administrator admin,@RequestParam("captcha") String captcha,@RequestParam("captchakey") String captchakey) {
    //管理员登录
        //验证是否正确
        String captchacheck= checkCaptcha(captchakey,captcha);
        if (!captchacheck.equals("success")){
            return Result.error(402,captchacheck);
        }
        //通过用户名查询用户的信息
        Administrator admin1=administratorService.selectByAccount(admin.getAccount());
        //如果有信息，就比对密码是否正确
        if(admin1!=null){
            //加密浏览器传入的数据和数据库中的密码进行比较
            boolean check=DeCodeUtil.deCode(admin.getPassword()).equals(admin1.getPassword());
            if (check){
                //如果对比正确登陆成功，签发token
                String token= JWTUtil.getToken();
                return Result.success("登陆成功",token);
            }
        }
        return Result.error(401,"认证失败");
        //如果比对成功，签发token

    }
    public String checkCaptcha(String captchakey, String captchaCode) {
        if (captchakey == null) {
            return "请求参数异常";
        }
        String code=(String) redisService.get(captchakey);
        if (code==null){
            return "验证码已过期";
        }
        if (captchaCode.equalsIgnoreCase(code)){
            return "success";
        }
        return "验证码验证失败";
    }
}
